package com.dtsz.m2.ui.activity;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.refresh.LoadingFooter;
import com.dtsz.m2.ui.adapter.NoticeAdatper;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.Notice;
import com.dtsz.m2.volley.state.HUCState;

import java.util.ArrayList;
import java.util.List;

public class MiningRecordActivity extends BaseActivity implements com.dtsz.m2.refresh.xRecyclerView.OnRefreshListener {
    private com.dtsz.m2.refresh.xRecyclerView xRecyclerView;
    private NoticeAdatper noticeAdatper;
    private List<Notice> noticeList = new ArrayList<>();
    private boolean refreshFlag = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_mining_record);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("采矿记录");
        noticeAdatper = new NoticeAdatper(this, noticeList, R.layout.item_notice_dateils);
        xRecyclerView.setAdapter(this, noticeAdatper, new LinearLayoutManager(this));
        xRecyclerView.setOnRefreshListener(this);
    }

    @Override
    public void initView() {
        super.initView();
        xRecyclerView = findViewById(R.id.xrecyclerView);
    }

    /**
     * 下拉刷新
     *
     * @param refreshLayout
     */
    @Override
    public void refresh(SwipeRefreshLayout refreshLayout) {
        refreshFlag = false;
        onStart();
    }

    /**
     * 上拉加载
     *
     * @param recyclerView
     */
    @Override
    public void loadMore(RecyclerView recyclerView) {
        refreshFlag = true;
        int CurrentCount = Integer.parseInt(pageVo.getCurrent()) + 1;
        pageVo.setCurrent(CurrentCount + "");
        pageVo.setSize(10 + "");
        getInitData();
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object obj) {
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (obj instanceof List) {
                    List<Notice> noticeLists = (List<Notice>) obj;
                    if (refreshFlag) {
                        if (noticeLists == null || noticeLists.size() == 0) {
                            if (noticeList.size() == 0) {
                                xRecyclerView.refreshComplete(LoadingFooter.State.NoData, "");
                            } else {
                                xRecyclerView.refreshComplete(LoadingFooter.State.TheEnd, "");
                            }
                        } else {
                            for (Notice notice : noticeLists) {
                                noticeList.add(notice);
                            }
                            noticeAdatper.notifyDataSetChanged();
                            xRecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
                        }
                    } else {
                        noticeList.clear();
                        for (Notice notice : noticeLists) {
                            noticeList.add(notice);
                        }
                        noticeAdatper.notifyDataSetChanged();
                        xRecyclerView.refreshComplete(LoadingFooter.State.Normal, "");
                    }
                }
                break;
        }
        loadDismiss();
    }

    @Override
    protected void onStart() {
        super.onStart();
        refreshFlag = false;
        pageVo.setCurrent(1 + "");
        pageVo.setSize(10 + "");
        getInitData();
    }

    private void getInitData() {
        pageVo.setIndex("2");
        hucRequestInterfase.executeStr(this, Notice.class,
                HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.NOTICES_URL,
                hucParameInterfase.getRequestParameter(pageVo), false);
    }

}
